<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Product extends CI_Controller {

    private $_page=6;
    public function __construct(){
        parent::__construct();
        $this->_utility();
    }
	public function index()
	{

		$config['uri_segment'] = 2;//参数三为当前页
		$config['anchor_class'] = ""; 
		$config['base_url'] = site_url('product');
		$config['total_rows'] =$this->db->count_all('Product');
		$config['per_page'] = $this->_page;
		 $uri[2]=$this->uri->segment(2);
		 
		 $total=ceil($config['total_rows']/$config['per_page']);
		 if($uri[2]>$total){
		 	show_404();
		 }
		 		 
		$this->pagination->initialize($config); 	
		
		$list=$this->Product->getallproduct($config['per_page'],$uri[2]);		

		$pager=$this->pagination->create_links();	

		$this->MySmarty->assign('pager',$pager);  
        $this->MySmarty->assign('list',$list);

		$in_cache_name=md5("allproduct".$uri[2]);
		$this->MySmarty->display("products.html",$in_cache_name,$this->Config['ProductTitle'],$this->Config['ProductKeywords'],$this->Config['ProductDescription']);
	}

	public function cid()
	{
	    $cid=$this->uri->segment(3);	
		$cid=$this->Product->getproductcategory('16',$cid);
	    $uri[4]=$this->uri->segment(4);
	    $uri[2]=$this->uri->segment(2);
		$config['uri_segment'] = 4;
		$config['anchor_class'] = ""; 
		$config['base_url'] = site_url('product/cid/'.$cid);
		$config['total_rows'] =$this->Product->categorycount($cid); 
		$config['per_page'] = $this->_page;
	
		$total=ceil($config['total_rows']/$config['per_page']);
		 if( $uri[4]>$total ){
		 	show_404();
		 }
		
		$this->pagination->initialize($config);
		
		$list=$this->Product->getcidproduct($cid,$config['per_page'],$uri[4]);		
		
		$pager=$this->pagination->create_links();					

		$this->MySmarty->assign('pager',$pager);  
        $this->MySmarty->assign('list',$list); 

		$in_cache_name=md5("product".$cid.$uri[2].$uri[4]);
        $this->MySmarty->display("products.html",$in_cache_name,$this->Config['ProductTitle'],$this->Config['ProductKeywords'],$this->Config['ProductDescription']);
	}
	
	public function search()
	{
	    $uri[3]=$this->uri->segment(3);
	    $keyword=$this->input->get('keyword');
	    if($keyword==""){$keyword="请输入关键词";}
		$config['uri_segment'] = 3;//参数三为当前页
		$config['url_keyword'] = "/?keyword=".$keyword; 
		$config['base_url'] = site_url('product/search/');
		$config['total_rows'] =$this->Product->getsearchproduct(0,0,1);
		$config['per_page'] = $this->_page;

		$total=ceil($config['total_rows']/$config['per_page']);
		 if($uri[3]>$total){
		 	show_404();
		 }
		
		$this->pagination->initialize($config); 	
		
		$list=$this->Product->getsearchproduct($config['per_page'],$uri[3]);		
		
		$pager=$this->pagination->create_links();	
		$this->MySmarty->caching=false;
		$this->MySmarty->assign('categoryname',"| 搜索结果");
		$this->MySmarty->assign('pager',$pager);
        $this->MySmarty->assign('list',$list); 
		$in_cache_name=md5("Products_search".$uri[3]);
        $this->MySmarty->display("products.html",$in_cache_name,$this->Config['ProductTitle'],$this->Config['ProductKeywords'],$this->Config['ProductDescription']);
    }
	
	
	
	public function id()
	{
	 	$uri[3]=$this->uri->segment(3);
	 	$rs=$this->Product->getoneproduct($uri[3]);

		$categoryname=$this->Product->getcategoryname($rs['CategoryID']);
		$this->MySmarty->assign('webtitle',$rs['ProductName']." ".$categoryname);
		$this->MySmarty->assign('categoryname',"| ".$categoryname);
		$this->MySmarty->assign('rs',$rs);
		$in_cache_name=md5("productdetail".$uri[3]);
        $this->MySmarty->display("Products_detail.html",$in_cache_name,$rs['ProductName'].'|'.$this->Config['ProductTitle'],$this->Config['ProductKeywords'],$this->Config['ProductDescription']);
    }
	
	
	public  function _utility(){

		$this->load->model('Product_model','Product');
		$this->load->library('pagination');

		$uri[3]=$this->uri->segment(3);
		$categoryname=$this->Product->getcategoryname($uri[3]);
		if($categoryname==''){
            $categoryname='产品中心';
        }
        $keyword=$this->input->get('keyword');
        if($keyword==""){$keyword="请输入关键词";}
        $this->MySmarty->assign('keyword',$keyword);
        $this->Config['ProductTitle']=$categoryname.'|'.$this->Config['ProductTitle'];
        $this->MySmarty->assign('categoryname',"| ".$categoryname);
	}
	

}

